//有序 无重复
//红黑树 自动排序
package com.itheima;

import java.util.Comparator;
import java.util.TreeSet;

public class TreeSetDemo {
    public static void main(String[] args) {
        test1();
        test2();
        int sum = sum(1, 2, 3, 4, 5);
        System.out.println(sum);
    }

    private static void extracted() {
        //只能存引用类型
        TreeSet<Integer> set = new TreeSet<>();
        //包装为integer类型    自动装箱
        set.add(3);
        set.add(5);
        set.add(2);
        set.add(1);
        set.add(4);

        for (Integer i : set) {
            System.out.println(i);
        }
        System.out.println(set);
    }

    private static void test1() {
        System.out.println("------1------");
        TreeSet<Student> set1 = new TreeSet<>();
        set1.add(new Student("A", 19));
        set1.add(new Student("B", 18));
        set1.add(new Student("C", 20));
        set1.add(new Student("D", 21));

        for (Student s : set1) {
            System.out.println(s.getName()+"---"+s.getAge());
        }

    }

    private static void test2() {
        System.out.println("------2------");
        TreeSet<Student> set2 = new TreeSet<>();
        set2.add(new Student("A", 19));
        set2.add(new Student("B", 18));
        set2.add(new Student("C", 20));
        set2.add(new Student("D", 21));
//        Comparator<Student> comparable = new Comparator<Student>() {
//            @Override
//            public int compare(Student o1, Student o2) {
//                return o1.getAge() - o2.getAge();
//            }
//        };


        Comparator<Student> comparable = (o1, o2) -> o1.getAge() - o2.getAge();

        for (Student s : set2){
            System.out.println(s.getName()+"---"+s.getAge());
        }
    }


//可变参数必须在形参后面（...a为数组）
    public static int sum(int ...a){
        System.out.println("------3------");
        int total = 0;
        for (int i : a){
            total += i;
        }
        return total;
    }
}
